package com.wise.wing.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.bm.corelibs.views.ScrollerNumberPicker;
import com.bm.corelibs.views.ScrollerNumberPicker.OnSelectListener;
import com.wise.wing.R;
import com.wise.wing.bean.TCity;





public class CityPicker extends LinearLayout {
	
	private ScrollerNumberPicker picker1;
	private ScrollerNumberPicker picker2;
	private ScrollerNumberPicker picker3;
	private String selected;
	
	private List<TCity> tProvices;
	private List<TCity> tCities;
	private List<TCity> tAreas;
	private ArrayList<String> provices = new ArrayList<String>();
	private ArrayList<String> cities = new ArrayList<String>();
	private ArrayList<String> areas = new ArrayList<String>();
	private HashMap<String, Integer> indexer = new HashMap<String, Integer>();

	public CityPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CityPicker(Context context) {
		super(context);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		int height = LayoutInflater.from(getContext()).inflate(R.layout.layout_city_picker,
				this).getHeight();
		System.out.println(height + "height");
		picker1 = (ScrollerNumberPicker) findViewById(R.id.first_picker);
		picker2 = (ScrollerNumberPicker) findViewById(R.id.second_picker);
		picker3 = (ScrollerNumberPicker) findViewById(R.id.third_picker);
		
		picker1.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {}
			
			@Override
			public void endSelect(int id, String text) {
				initCities(id);
				picker2.setData(cities);
				picker2.setDefault(0);
				
				initAreas(0);
				picker3.setData(areas);
				picker3.setDefault(0);
			}
		});
		
		picker2.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {}
			
			@Override
			public void endSelect(int id, String text) {
				initAreas(id);
				picker3.setData(areas);
				picker3.setDefault(0);
			}
		});
		
		picker3.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {}
			
			@Override
			public void endSelect(int id, String text) {}
		});
	}

	public void setData(List<TCity> data){
		tProvices = data;
		initProvinces();
		picker1.setData(provices);
		picker1.setDefault(0);
		
		initCities(0);
		picker2.setData(cities);
		picker2.setDefault(0);
		
		initAreas(0);
		picker3.setData(areas);
		picker3.setDefault(0);
	}

	public String getFirstSelected() {
		selected = picker1.getSelectedText();
		return selected;
	}

	public String getSecondSelected() {
		if(picker2.getData().size() < 2) 
			selected = "";
		else
			selected = picker2.getSelectedText();
		return selected;
	}

	public String getThirdSelected() {
		selected = picker3.getSelectedText();
		return selected;
	}

	public String getSelectedAddress(){
		return getFirstSelected() + getSecondSelected() + getThirdSelected();
	}
	public int getAreaId(){
		String selected = getThirdSelected();
		return indexer.get(selected);
	}
	
	private void initProvinces(){
		provices.clear();
		for(TCity city : tProvices)
			provices.add(city.name);
	}
	
	private void initCities(int position) {
		cities.clear();
		TCity province = tProvices.get(position);
		tCities = province.nextArea;
		for(TCity city : tCities)
			cities.add(city.name);
		
		
	}
	
	private void initAreas(int position) {
		areas.clear();
		indexer.clear();
		TCity city = tCities.get(position);
		tAreas = city.nextArea;
		for(TCity tmp : tAreas) {
			areas.add(tmp.name);
			indexer.put(tmp.name, tmp.id);
		}
	}
	
}
